package com.shbaiche.bifenba.adapter.home;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shbaiche.bifenba.R;
import com.shbaiche.bifenba.ui.home.SearchActivity;

import java.util.List;

public class SearchHistoryAdapter extends BaseAdapter {
    private Context context;

    private List<String> his;


    public SearchHistoryAdapter(Context context, List<String> his) {
        super();
        this.context = context;
        this.his = his;
    }

    public void setData(List<String> his){
        this.his = his;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (his == null)
            return 0;
        return his.size();
    }

    @Override
    public String getItem(int arg0) {
        return his.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(final int position, View v, ViewGroup arg2) {
        ViewHolder holder = null;
        if (v == null) {
            holder = new ViewHolder();
            v = LayoutInflater.from(context).inflate(R.layout.adapter_search_item, null);
            holder.ic = (ImageView) v.findViewById(R.id.ic_search_x);
            holder.contentTv = (TextView) v.findViewById(R.id.pay_tv1_desc);
            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

        String item = getItem(position);
        holder.contentTv.setText(item);

        holder.ic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences mPref = context.getSharedPreferences("search",
                        Activity.MODE_PRIVATE);
                String history = mPref.getString(SearchActivity.KEY_SEARCH_HISTORY_KEYWORD, "");
                if (!TextUtils.isEmpty(history)) {
                    String[] array = history.split(",");
                    if (array != null && array.length > 0) {
                        try {
                            array[position] = "";
                        } catch (Exception e) {

                        }
                    }
                    String text = "";
                    for (int i = 0; i < array.length; i++) {
                        if (i != 0 && !"".equals(array[i])) {
                            if (!TextUtils.isEmpty(text)) {
                                text = text + "," + array[i];
                            } else {
                                text = array[i];
                            }
                        } else if (i == 0 && !"".equals(array[i])) {
                            text = array[i];
                        }
                    }

                    SharedPreferences.Editor editor = mPref.edit();
                    editor.putString(SearchActivity.KEY_SEARCH_HISTORY_KEYWORD, text);
                    editor.commit();

                    if (his != null)
                        his.remove(position);

                    notifyDataSetChanged();

                }
            }
        });

        return v;
    }

    class ViewHolder {
        protected ImageView ic;

        protected TextView contentTv;
    }
}
